package com.itheima.service;

import com.itheima.mapper.ItemMapper;
import com.itheima.pojo.Item;
import com.itheima.util.SqlSessionFactoryUtils;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;

import java.util.List;

public class ItemService {
    SqlSessionFactory factory = SqlSessionFactoryUtils.getSqlSessionFactory();

    public List<Item> selectAll() {
        //调用ItemMapper.selectAll()

        //2. 获取SqlSession
        SqlSession sqlSession = factory.openSession();
        //3. 获取ItemMapper
        ItemMapper mapper = sqlSession.getMapper(ItemMapper.class);

        //4. 调用方法
        List<Item> items = mapper.selectAll();

        sqlSession.close();

        return items;
    }

    public Item selectById(Integer id) {
        //调用ItemMapper.selectById()

        //2. 获取SqlSession
        SqlSession sqlSession = factory.openSession();
        //3. 获取ItemMapper
        ItemMapper mapper = sqlSession.getMapper(ItemMapper.class);

        //4. 调用方法
        Item item = mapper.selectById(id);

        sqlSession.close();

        return item;
    }

    public void add(Item item) {
        //调用ItemMapper.add()

        //2. 获取SqlSession
        SqlSession sqlSession = factory.openSession();
        //3. 获取ItemMapper
        ItemMapper mapper = sqlSession.getMapper(ItemMapper.class);

        //4. 调用方法
        mapper.add(item);

        // 提交事务
        sqlSession.commit();
        // 释放资源
        sqlSession.close();
    }

    public List<Item> selectBySellerId(Integer sellerId) {
        //调用ItemMapper.selectBySellerId()

        //2. 获取SqlSession
        SqlSession sqlSession = factory.openSession();
        //3. 获取ItemMapper
        ItemMapper mapper = sqlSession.getMapper(ItemMapper.class);

        //4. 调用方法
        List<Item> items = mapper.selectBySellerId(sellerId);

        sqlSession.close();

        return items;
    }

    public void update(Integer id, String itemName, String description, Integer price, Integer inventory) {
        //调用ItemMapper.update()

        //2. 获取SqlSession
        SqlSession sqlSession = factory.openSession();
        //3. 获取ItemMapper
        ItemMapper mapper = sqlSession.getMapper(ItemMapper.class);

        //4. 调用方法
        mapper.update(id, itemName, description, price, inventory);

        // 提交事务
        sqlSession.commit();
        // 释放资源
        sqlSession.close();
    }

    public void deleteById(Integer id) {
        //调用ItemMapper.deleteById()

        //2. 获取SqlSession
        SqlSession sqlSession = factory.openSession();
        //3. 获取ItemMapper
        ItemMapper mapper = sqlSession.getMapper(ItemMapper.class);

        //4. 调用方法
        mapper.deleteById(id);

        // 提交事务
        sqlSession.commit();
        // 释放资源
        sqlSession.close();
    }

    public void updateSalesVolume(Integer salesVolume, Integer itemId) {
        //调用ItemMapper.updateSalesVolume()

        //2. 获取SqlSession
        SqlSession sqlSession = factory.openSession();
        //3. 获取ItemMapper
        ItemMapper mapper = sqlSession.getMapper(ItemMapper.class);

        //4. 调用方法
        mapper.updateSalesVolume(salesVolume, itemId);

        // 提交事务
        sqlSession.commit();
        // 释放资源
        sqlSession.close();
    }
}
